/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2021-11-20 15:22:55
 * 天凉好个秋
 */
const path = require("path");
import { Sequelize } from "sequelize-typescript";
import config from "../config/index";
import { dbLogger } from "../logger";
const { db_name, db_host, db_port, db_user, db_password } = config.db;

export const sequelize = new Sequelize(db_name as string, db_user as string, db_password, {
  host: db_host,
  port: db_port as unknown as number,
  dialect: "mysql",
  define: {
    timestamps: true,
    freezeTableName: true,
    deletedAt: false
  },
  logging: msg => dbLogger.info(msg),
  models: [path.join(__dirname, '..', 'model/**/*.ts'), path.join(__dirname, '..', 'model/**/*.js')]
});

// 连接数据库
const dbConnect = async () => {
  try {
    await sequelize.authenticate();
    console.log("数据库连接成功");
  } catch (error) {
    console.error("数据库连接失败:", error);
  }
};

export default dbConnect
